<?php

declare(strict_types=1);

namespace App\Controller;

use App\Service\EsService;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;


#[Controller(prefix:"/api/order")]
class OrderController extends AbstractController
{
    #[RequestMapping(path: "delete", methods: "get")]
    public function delete(EsService $service)
    {
        $id = $this->request->input('id');
        $res = $service->delete('order_index',$id);

        return ['ok'=>true, 'res'=>$res];
    }

    #[RequestMapping(path: "create", methods: "get")]
    public function create(EsService $service)
    {
        $res = $service->create('order_index','order_2812848',['name'=>'tom']);

        return ['ok'=>true, 'res'=>$res];
    }

    #[RequestMapping(path: "detail", methods: "get")]
    public function detail(EsService $service)
    {
        $id = $this->request->input('id');
        $res = $service->get('order_index','order_' . $id);

        return ['code'=>1, 'data'=>!empty($res['_source']) ? $res['_source'] : ''];
    }

    #[RequestMapping(path: "list", methods: "get")]
    public function list(EsService $service)
    {
        $page = (int)$this->request->input('page');
        $size = (int)$this->request->input('size',20);

        $res = $service->search('order_index');

        return ['code'=>1, 'data'=>$res];
    }
}
